import 'package:v50art/bean/UserBean.dart';
import 'package:v50art/service/CacheService.dart';
import 'package:v50art/service/NetworkService.dart';

class UserApi {
  static const _loginUrl = "consumer/app/login";
  static const _userCountUrl = "consumer/queryConsumerCount";
  static Future login({required String phone, required String password}) async {
    ResponseData? res = await NetworkService.post(url: _loginUrl, map: {
      "consumerPassword": password,
      "consumerPhone": phone,
    });
    UserBean? user = res.getTypedData((map) => UserBean.fromJson(map));
    if (user != null) {
      saveUser(user);
    }
    return null;
  }

  static Future<void> saveUser(UserBean res) async {
    await CacheService.putString("token", "${res.token}");
    await CacheService.putString("uid", "${res.uid}");
  }

  static Future<String> getUserCount() async {
    ResponseData? res = await NetworkService.post(url: _userCountUrl, map: {});
    return "${res.count}";
  }
}
